package org.sxp.modules.sys.controller;

import cn.hutool.core.util.StrUtil;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.sxp.common.utils.AeyeReflectionUtil;
import org.sxp.common.utils.Constant;
import org.sxp.common.utils.PageInfo;
import org.sxp.modules.sys.entity.SysUserEntity;
import org.sxp.modules.sys.shiro.ShiroUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Controller公共组件
 *
 *
 */
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected SysUserEntity getUser() {
		return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
	}

	protected Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpSession session;
	@Autowired(required = false)
	protected HttpServletResponse response;

	protected Long getUserId() {
		return this.getUser().getUserId();
	}

	protected String getDeptId() {
		return this.getUser().getDeptId();
	}

	protected HttpSession getSession() {
		return this.session;
	}

	protected HttpServletRequest getRequest() {
		return this.request;
	}

	protected HttpServletResponse getResponse() {
		return this.response;
	}

	protected boolean isAdmin(){
		return ShiroUtils.isAdmin();
	}

	protected PageInfo buildPageInfo() {
		String pageNum = this.request.getParameter("page");
		String pageSize = this.request.getParameter("limit");
		String orderField = this.request.getHeader(AeyeReflectionUtil.getFieldName(PageInfo::getOrderField));
		String orderType = this.request.getHeader(AeyeReflectionUtil.getFieldName(PageInfo::getOrderType));


		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNum(StrUtil.isBlank(pageNum) ? 1 : Integer.parseInt(pageNum));
		pageInfo.setPageSize(StrUtil.isBlank(pageSize) ? 10 : Integer.parseInt(pageSize));
		pageInfo.setOrderField(orderField);
		pageInfo.setOrderType(orderType);
		return pageInfo;
	}
}
